<template>
    <div class="table">
        <div class="container">
            <div class="handle-box" style="text-align: right">
                <!--<el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>-->
                <!--<el-button type="primary" icon="search" @click="search">搜索</el-button>-->
                <el-button type="primary" @click="add" style="">添加便民信息</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable">
                <el-table-column prop="title" label="标题">
                </el-table-column>
                <el-table-column prop="content" label="内容">
                </el-table-column>
                <el-table-column label="启禁用" width="150">
                    <template slot-scope="scope">
                        <span>
                            <el-switch
                                v-model="scope.row.status==0"
                                @click.native="openClose(scope.row)"
                                active-text="启用"
                                inactive-text="禁用">
                            </el-switch>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="是否发布">
                    <template slot-scope="scope">
                        <span v-if="scope.row.is_publish == '0'">未发布</span>
                        <span v-if="scope.row.is_publish == '1'">已发布</span>
                    </template>
                </el-table-column>
                <el-table-column prop="publish_time" label="发布时间">
                </el-table-column>
                <el-table-column prop="create_time" sortable label="创建时间">
                </el-table-column>
                <el-table-column prop="create_user_name" label="创建人名称">
                </el-table-column>
                <el-table-column label="操作" width="180" align="center" fixed="right">
                    <template slot-scope="scope">
                        <div>
                            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button type="text" icon="el-icon-position" @click="release(scope.$index, scope.row)">发布</el-button>
                            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <!--<div class="pagination">-->
            <!--<el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next" :total="1000">-->
            <!--</el-pagination>-->
            <!--</div>-->
            <div class="block">
                <span class="demonstration"></span>
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page.sync="currentPage1"
                    :page-size="pageSize"
                    layout="total, prev, pager, next"
                    :total="totalRow">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="道具" :visible.sync="editVisible" width="50%">
            <convenientAdd :uid="convenientId" ref="convenientAdd" @done="convenientIdDone"></convenientAdd>
        </el-dialog>
    </div>
</template>

<script>
    import convenientAdd from '@/components/convenient/Convenient-add'
    export default {
        components:{
            convenientAdd,
        },
        name: 'basetable',
        data() {
            return {
                select_word: '',
                tableData: [],
                cur_page: 1,
                multipleSelection: [],
                editVisible: false,

                convenientId: '',

                pageSize: 0,
                totalRow: 0,
                currentPage1: 0,
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
            }
        },
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 分页导航
            handleCurrentChange(val) {
            },
            // 获取列表数据
            getData(pageNumber) {
                this.loadopen();
                this.config.post('/housinggestate/housinggestate/convenience/findPage', {
                    pageNum: pageNumber?pageNumber:1,
                }).then(res=>{
                    this.loadclose();
                    if(res.data.code==='0'){
                        this.tableData = res.data.page.list;
                        this.pageSize = res.data.page.pageSize;
                        this.totalRow = res.data.page.totalRow;
                        this.currentPage1 = res.data.page.pageNumber;
                        // this.tableData = res.data.roleList;
                    }else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                })
            },
            search() {
            },
            // 分页导航
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.getData(val);
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
            },
            // 启用禁用
            openClose(row){
                // let sta = row.state==='0'?'1':'0';
                this.config.post('/housinggestate/housinggestate/convenience/changeStatus', {id: row.id}).then(res=>{
                    if(res.data.code==='0'){
                        this.$message.success(res.data.message);
                        this.getData();
                    }else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                })
            },
            // 添加
            add(){
                this.editVisible = true;
                this.convenientId = '';
                setTimeout(()=>{
                    this.$refs.convenientAdd.getInfo();
                }, 100);
            },
            // 编辑等级
            handleEdit(index, row) {
                this.editVisible = true;
                this.convenientId = row.id;
                setTimeout(()=>{
                    // console.log("走到这了")
                    this.$refs.convenientAdd.getInfo();
                }, 100);
            },
            handleDelete(index, row) {
                this.$confirm('确定删除此便民信息?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.loadopen();
                    // console.log(row.id)
                    this.config.post('/housinggestate/housinggestate/convenience/delete', {id: row.id}).then(res=>{
                        this.loadclose();
                        if(res.data.code==='0'){
                            this.$message.success(res.data.message);
                            this.getData();
                        }else {
                            this.$message.error(this.config.handleError(res.data));
                        }
                    });
                }).catch(() => {
                });
            },
            release(index, row) {
                this.$confirm('确定发布此便民信息?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // console.log(row.is_publish);
                    if(row.is_publish == '0'){
                        this.loadopen();
                        // console.log(row.id)
                        this.config.post('/housinggestate/housinggestate/convenience/changePublish', {id: row.id}).then(res=>{
                            this.loadclose();
                            if(res.data.code==='0'){
                                this.$message.success(res.data.message);
                                this.getData();
                            }else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                        });
                    }else{
                        this.$message.error('该便民信息已发布，请勿重复发布！');
                    }
                }).catch(() => {
                });
            },
            convenientIdDone(str) {
                this.editVisible = false;
                this.getData();
            },
            // 保存编辑
            saveEdit() {
            },
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
</style>
